(function() {
	if (!localStorage.getItem('token')) {
		alert('非法登录');
		location.href = 'index.html';
		return;
	};

	// let oQueryBtn = document.querySelector('.query-btn');
	// let oQueryOrderBtn = document.querySelector('.query-order-btn');

	// 获取订单号
	let tradeNo = yzb.getUrlValue('trade_no');

	// 查询支付结果
	yzb.ajax({
		method: 'post',
		url: BASE_URL + '/api_payquery',
		data: {
			userId: localStorage.getItem('token'),
			tradeNo,
		},
		success(res) {
			if (res.code != 0) {
				console.log(res);
				return;
			};
			// 查询成功 获取用户订单列表
			getOrderList();
		}
	});

	// 页面刷新获取订单列表
	getOrderList();

	// 订单列表
	function getOrderList() {

		let oMypayList = document.querySelector('.mypay-list');
		
		oMypayList.innerHTML = '';

		yzb.ajax({
			method: 'post',
			url: BASE_URL + '/api_order',
			data: {
				status: 'vieworder',
				userId: localStorage.getItem('token')
			},
			success(res) {
				if (res.code != 0) {
					console.log(res);
					return;
				};

				// dom
				let orderData = res.data;
				orderData.forEach(item => {
					let strLi = '';
					let strGoods = '';
					let priceAll = 0;
					// 商品遍历
					item.goods.forEach(goodsItem => {
						strGoods += `
								<div class="goods-list">
									<img src="${goodsItem.goods_thumb}" alt="">
									<span>${goodsItem.goods_name}</span>
									<span>${goodsItem.price}</span>
									<span>${goodsItem.goods_number}</span>
									<span>${parseInt(goodsItem.goods_number) * goodsItem.price}</span>
								</div>
								`;
						priceAll += parseInt(goodsItem.goods_number) * goodsItem.price;
					});

					strLi += strGoods;
					strLi += `
								<div class="price-all">$${priceAll}</div>
								<div class="address">${JSON.stringify(item.order_address)}</div>
								<div class="order-id">${item.trade_no}</div>
								<div class="order-status">${item.status}</div>
								<div class="del-order" trade-no="${item.trade_no}">删除按钮</div>
							`;

					let li = document.createElement('li');
					li.innerHTML = strLi;
					oMypayList.appendChild(li);
				});
				
				// 调用一次头部购物车方法
				getCartValue();

				// 选择删除按钮
				let aDelBtn = document.querySelectorAll('.mypay-list .del-order');
				aDelBtn.forEach(item=>{
					item.onclick = function() {
						yzb.ajax({
							method:'post',
							url:BASE_URL + '/api_order',
							data:{
								status:'delorder',
								userId:localStorage.getItem('token'),
								tradeNo:this.getAttribute('trade-no')
								},
								success :res => {
									if(res.code != 0){
										console.log(res);
										return;
									};
									this.parentNode.remove();
								}
						})
					}
				})
			},
		})
	};
})();